﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;

namespace NetTopologySuite.Operation.Buffer
{
	public class OffsetCurveVertexList
	{
		public OffsetCurveVertexList()
		{
			this._ptList = new List<Coordinate>();
		}

		public IPrecisionModel PrecisionModel
		{
			get
			{
				return this._precisionModel;
			}
			set
			{
				this._precisionModel = value;
			}
		}

		public double MinimumVertexDistance
		{
			get
			{
				return this._minimimVertexDistance;
			}
			set
			{
				this._minimimVertexDistance = value;
			}
		}

		public void AddPt(Coordinate pt)
		{
			Coordinate coordinate = new Coordinate(pt);
			this._precisionModel.MakePrecise(coordinate);
			if (this.IsDuplicate(coordinate))
			{
				return;
			}
			this._ptList.Add(coordinate);
		}

		private bool IsDuplicate(Coordinate pt)
		{
			if (this._ptList.Count < 1)
			{
				return false;
			}
			Coordinate c = this._ptList[this._ptList.Count - 1];
			return pt.Distance(c) < this._minimimVertexDistance;
		}

		public void CloseRing()
		{
			if (this._ptList.Count < 1)
			{
				return;
			}
			Coordinate coordinate = new Coordinate(this._ptList[0]);
			Coordinate other = this._ptList[this._ptList.Count - 1];
			if (coordinate.Equals(other))
			{
				return;
			}
			this._ptList.Add(coordinate);
		}

		public Coordinate[] Coordinates
		{
			get
			{
				if (this._ptList.Count > 1)
				{
					Coordinate coordinate = this._ptList[0];
					Coordinate other = this._ptList[this._ptList.Count - 1];
					if (!coordinate.Equals(other))
					{
						this.AddPt(coordinate);
					}
				}
				return this._ptList.ToArray();
			}
		}

		public override string ToString()
		{
			return new GeometryFactory().CreateLineString(this.Coordinates).ToString();
		}

		private readonly List<Coordinate> _ptList;

		private IPrecisionModel _precisionModel;

		private double _minimimVertexDistance;
	}
}
